<?php

// Acceso restringido
defined('JL') or die('Acceso restringido');

class ActualiteModel extends JLModelBase {

    /**
     * Retrona Actualidad
     * @Retorna array
     */
    public function SelectActualite($_id) {
        $_data = null;

        $sql = "SELECT 
            `int_actualite`,
            `var_ac_title`,
            `txt_content_actualite`,
            `dat_creation_act`,
            `dat_modification_act`,
            `var_monthyear`,
            `int_id_user`,
            `int_id_section`,
            `txt_image_galleries`,
            `int_id_state`
            FROM 
                `tbl_actualites` WHERE `int_actualite` = " . $this->DB->Quote($_id) . "";

        $this->DB->executeQuery($sql);

        if ($this->DB->getRowsAffected() > 0) {
            $_data = $this->DB->getDataArray();
            $_data = $_data[0];
        }

        return $_data;
    }

    /**
     * Inserta Actualidad
     * @Retorna bool
     */
    public function InsertActualite($var_ac_title, $txt_content_actualite, $dat_creation_act, $dat_modification_act, $int_id_user, $int_id_section, $var_monthyear, $int_id_state, $order) {
        $sql = "INSERT INTO `tbl_actualites` (
                `var_ac_title`,
                `txt_content_actualite`,
                `dat_creation_act`,
                `dat_modification_act`,
                `var_monthyear`,
                `int_id_user`,
                `int_id_section`,
                `int_id_state`,
                `txt_image_galleries`
                ) VALUE(
                    " . $this->DB->Quote($var_ac_title) . ",
                    " . $this->DB->Quote($txt_content_actualite) . ",
                    " . $this->DB->Quote($dat_creation_act) . ",
                    " . $this->DB->Quote($dat_modification_act) . ",
                    " . $this->DB->Quote($var_monthyear) . ",
                    " . $this->DB->Quote($int_id_user) . ",
                    " . $this->DB->Quote($int_id_section) . ",
                    " . $this->DB->Quote($int_id_state) . ",
                    " . $this->DB->Quote($order) . ")";
        $this->DB->executeQuery($sql);

        if ($this->DB->getRowsAffected() > 0) {
            $_true = true;
        } else {
            $_true = false;
        }

        return $_true;
    }

    /**
     * Modifica Actualidad
     * @Retorna bool
     */
    public function UpdateActualite($_id, $var_ac_title, $txt_content_actualite, $dat_modification_act, $int_id_section, $int_id_state, $order) {
        $sql = "UPDATE `tbl_actualites`  
                SET 
                    `var_ac_title` = " . $this->DB->Quote($var_ac_title) . ",
                    `txt_content_actualite` = " . $this->DB->Quote($txt_content_actualite) . ",
                    `dat_modification_act` = " . $this->DB->Quote($dat_modification_act) . ",
                    `int_id_section` = " . $this->DB->Quote($int_id_section) . ",
                    `int_id_state` = " . $this->DB->Quote($int_id_state) . " ,
                    `txt_image_galleries`= " . $this->DB->Quote($order) . " 
                WHERE  `int_actualite` = " . $this->DB->Quote($_id) . "";

        $this->DB->executeQuery($sql);

        if ($this->DB->getRowsAffected() > 0) {
            $_true = "Données sauvegarder avec succès";
        } else {
            $_true = "Ne peut enregistrer vos données.";
        }

        return $_true;
    }

    /**
     * Elimina Actualidad
     * @Retorna bool
     */
    public function DeleteActualite($_id) {
        $sql = "DELETE FROM 
			  `tbl_actualites` 
			WHERE 
			  `int_actualite` = " . $this->DB->Quote($_id) . " AND `int_id_state` <> 1";

        $this->DB->executeQuery($sql);

        if ($this->DB->getRowsAffected() > 0) {
            $_true = true;
        } else {
            $_true = false;
        }

        return $_true;
    }

    function getGroupActualites() {
        $sql = "SELECT var_monthyear FROM tbl_actualites
        WHERE int_id_section < 3 && int_id_section !=0 && int_id_state != 3
        GROUP BY var_monthyear
        ORDER BY int_actualite DESC
        LIMIT 200";

        $this->DB->executeQuery($sql);
        $data = NULL;

        if ($this->DB->getRowsAffected() > 0) {
            $data = $this->DB->getDataArray();
        }

        return $data;
    }

}